package com.example.mqdemo.rabbit;

import com.example.mqdemo.common.ExchangeConstant;
import com.example.mqdemo.common.QueueConstant;
import com.example.mqdemo.common.RoutingConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 队列绑定配置
 */
@Configuration
public class BindingConfig {

    /**
     * 绑定队列到交换机上
     * massage -> routingKey -> exchange -> queue -> consumer
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding orderBinding(@Qualifier(QueueConstant.orderQueueName) Queue queue,
                                @Qualifier(ExchangeConstant.orderChangeName) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RoutingConstant.orderRoutingKey).noargs();
    }

    /**
     * 绑定取消订单的交换机和队列
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding orderCancelBinding(@Qualifier(QueueConstant.orderCancelQueueName) Queue queue,
                                      @Qualifier(ExchangeConstant.orderCancelChangeName) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RoutingConstant.orderCancelRoutingKey).noargs();
    }

    /**
     * 绑定取消订单的死信交换机和死信队列
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding orderCancelDeadBinding(@Qualifier(QueueConstant.orderCancelDeadQueueName) Queue queue,
                                          @Qualifier(ExchangeConstant.orderCancelDeadChangeName) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RoutingConstant.orderCancelDeadRoutingKey).noargs();
    }

}
